import Vue from 'vue'
import App from './App'

//uview
import uView from 'uview-ui';
Vue.use(uView);

//顶部bar配置
import navbarConfig from './static/js/navbar.js'
Vue.prototype.navbarConfig = navbarConfig

//颜色配置
import colorConfig from './static/js/color.js'
Vue.prototype.colorConfig = colorConfig

//顶部bar
import hxNavbar from "./components/hx-navbar/hx-navbar.vue"
Vue.component('hx-navbar', hxNavbar)

import * as config from './config'
//api
const msg = (title, duration = 2000, mask = false, icon = 'none') => {
	//统一提示方便全局修改
	if (Boolean(title) == false) {
		return
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	})
}
let userInfo = undefined
const logout = () => {
	userInfo = undefined
	uni.removeStorage({
		key: 'userInfo'
	})
}
const setUserInfo = (i) => {
	userInfo = i
}
const request = (method, api, data = {}, load = true, content = 'application/json') => {
	//异步请求数据
	return new Promise(resolve => {
		if (!userInfo || !userInfo.token) {
			userInfo = uni.getStorageSync('userInfo')
		}
		let baseUrl = config.def().baseUrl
		let token = userInfo.token || ''
		load && uni.showLoading()
		uni.request({
			url: baseUrl + api,
			data: {
				...data
			},
			method: method,
			header: {
				'Content-Type': content,
				'token': token
			},
			success: (res) => {
				load && uni.hideLoading()
				console.log(res.data)
				if (res.statusCode == 200 && res.data.code == 200) {
					resolve(res.data)
				} else {
					if (res.data.message.indexOf('Token为空') == '-1') {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: '请先登录',
							icon: 'none'
						})
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}
				}
			},
			fail: (res) => {
				load && uni.hideLoading()
			}
		})
	})
}
const prePage = () => {
	let pages = getCurrentPages()
	let prePage = pages[pages.length - 2]
	// #ifdef H5
	return prePage
	// #endif
	return prePage.$vm
}
Vue.prototype.$api = {
	msg,
	request,
	logout,
	setUserInfo,
	prePage
}
const sliceTime = (time) => { //处理时间 返回 06-30 10:40
	return time && time.slice(5, 16).replace('T', ' ')
}
Vue.prototype.$util = {
	sliceTime
}

//vuex
import store from './store'
Vue.prototype.$store = store


Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()
